#!/usr/bin/env python
# coding: utf-8

# ## 复习21点
# ### 需求：
# * 两个玩家，游戏开始先输入名字
# * 用字典保存每个玩家信息：名字，获胜次数
# * 电脑随机产生两个数，每个玩家轮流猜1个数，与电脑随机产生的两个数求和，最接近21点的获胜
# * 每轮结束显示玩家信息
# * 按 q 退出游戏

# In[ ]:


# 游戏开场信息
import time
print('欢迎参加21点游戏'.center(36,'-'))
time.sleep(1)
print('游戏规则:')
time.sleep(1)
print('* 游戏需要2名玩家')
time.sleep(1)
print('* 每位玩家输入名字')
time.sleep(1)
print('* 玩家轮流猜一个数')
time.sleep(1)
print('* 与电脑随机生成的两个数求和')
time.sleep(1)
print('* 最接近21点的获胜')
time.sleep(1)
print('* 每局结束显示玩家信息：姓名和获胜次数')
time.sleep(1)


import random
game_dict = {}  # 定义玩家信息字典
score01 = 0
score02 = 0
dogfall = 0
game_numbers = 0
# 选择游戏进程
print('继续游戏，请按:y；按非y任意键可退出 '.center(28,'-'))
choose = input('')
while choose == 'y':
    game_numbers += 1
    name01 = input('玩家1名字：')
    name02 = input('玩家2名字：')
    if name01 == 'q' or name02 == 'q':
        print('退出游戏')
        break
    if name01 == '' or name02 == '':
        print('必须输入2名玩家，名字不能为空！退出游戏，请输入q')
        continue
    Num1 = random.randint(2,9)
    num_list =[1,10,11]
    Num2 = random.choice(num_list)
    player01 = input(f'{name01}请输入0-10的整数 ：')
    player02 = input(f'{name02}请输入0-10的整数 ：')
    
    if player01 == 'q' or player02 == 'q':
        print('谢谢参与！')
        break
        
    sum01 = Num1 + Num2 + int(player01)
    sum02 = Num1 + Num2 + int(player02)
        
    if sum01 > 21 and sum02 >21:
        print(f'{name01}的点数{sum01}，{name02}的点数{sum02}')
        print('都爆牌了，请重新输入')
        continue
        
    if sum01 == sum02 <= 21:
        dogfall += 1
        print(f'{name01}的点数{sum01}，{name02}的点数{sum02}')
        print('平局，请重新输入')
        continue
        
    if sum01 <= 21 and sum02 > 21:
        score01 += 1
        print(f'{name01}的点数{sum01}，{name02}的点数{sum02}')
        
    if sum01 > 21 and sum02 <= 21:
        score02 += 1
        print(f'{name01}的点数{sum01}，{name02}的点数{sum02}')
    
    if sum02 < sum01 <= 21:
        score01 += 1
        print(f'{name01}的点数{sum01}，{name02}的点数{sum02}')
        
    if sum01 < sum02 <= 21:
        score02 += 1
        print(f'{name01}的点数{sum01}，{name02}的点数{sum02}')
        
    
    fisrt_player = f'获胜{score01}局'
    second_player = f'获胜{score02}局'  
    game_dict[name01] = fisrt_player
    game_dict[name02] = second_player    
    print('玩家对战信息'.center(36,'-'))
    time.sleep(1)
    print(f'第{game_numbers}局：出现{dogfall}次平局')
    time.sleep(1)
    print(game_dict)
    
    
else :
    print('退出游戏')

